import React from 'react';
import vkbeautify from 'vkbeautify';
import { validateXML } from '../../../utils/validate';

export const isXml = (value) => !validateXML(value).error_code;
export const isJson = (value) => {
  try {
    return typeof JSON.parse(value) === 'object';
  } catch (error) {
    return false;
  }
};

export default function FormatValue(props) {
  const { type, data = '' } = props;
  return (
    <pre style={{ margin: 0, flex: 1, overflow: 'auto' }}>
      {data &&
        (type
          ? isXml(data)
            ? vkbeautify.xml(data)
            : data
          : isJson(data)
          ? JSON.stringify(JSON.parse(data), null, 4)
          : data)}
    </pre>
  );
}
